package pv;

import java.util.concurrent.TimeUnit;

/**
 * @author yezh
 * @date 2024/7/4 19:22
 */
public class SleepLoopThread extends Thread {

    public SleepLoopThread(Runnable runnable) {
        this(runnable, 2);
    }

    public SleepLoopThread(Runnable runnable, int sleepSec) {
        this(runnable, sleepSec, null, null);
    }

    public SleepLoopThread(Runnable runnable, int sleepSec, Integer loop, String tName) {
        super(() -> {
            try {
                Integer finalLoop = loop;
                if (finalLoop == null) {
                    while (true) {
                        runnable.run();
                        TimeUnit.SECONDS.sleep(sleepSec);
                    }
                } else {
                    while (finalLoop-- > 0) {
                        runnable.run();
                        TimeUnit.SECONDS.sleep(sleepSec);
                    }
                }
            } catch (InterruptedException e) {
            }
        }, tName);
    }

}
